home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / GraphicViewers / pCD / Source / hpcdtoppm.0.4 / tools.c.ORIG < prev   
Text File  |  1993-03-23  |  6KB  |  309 lines

  1. /* hpcdtoppm (Hadmut's pcdtoppm) v0.4
  2. *  Copyright (c) 1992, 1993 by Hadmut Danisch (danisch@ira.uka.de).
  3. *  Permission to use and distribute this software and its
  4. *  documentation for noncommercial use and without fee is hereby granted,
  5. *  provided that the above copyright notice appear in all copies and that
  6. *  both that copyright notice and this permission notice appear in
  7. *  supporting documentation. It is not allowed to sell this software in 
  8. *  any way. This software is not public domain.
  9. */
  10.  
  11. #include "hpcdtoppm.h"
  12.  
  13.  
  14. void clear(l,n)
  15.   implane *l;
  16.   int n;
  17. { dim x,y;
  18.   uBYTE *ptr;
  19.  
  20.   ptr=l->im;
  21.   for (x=0;x<l->mwidth;x++)
  22.     for (y=0; y<l->mheight;y++)
  23.       *(ptr++)=n;
  24. }
  25.  
  26.  
  27.  
  28.  
  29. void halve(p)
  30.   implane *p;
  31.  {dim w,h,x,y;
  32.   uBYTE *optr,*nptr;
  33.  
  34.   melde("halve\n");
  35.   if ((!p) || (!p->im)) error(E_INTERN);
  36.  
  37.   w=p->iwidth/=2;      
  38.   h=p->iheight/=2;     
  39.  
  40.  
  41.   for(y=0;y<h;y++)
  42.    {
  43.     nptr=(p->im) +   y*(p->mwidth);
  44.     optr=(p->im) + 2*y*(p->mwidth);
  45.  
  46.     for(x=0;x<w;x++,nptr++,optr+=2)
  47.      { *nptr = *optr;
  48.      }
  49.  
  50.    }
  51.  
  52.  }
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. void interpolate(p)
  61.   implane *p;
  62.  {dim w,h,x,y,yi;
  63.   uBYTE *optr,*nptr,*uptr;
  64.  
  65.   melde("interpolate\n");
  66.   if ((!p) || (!p->im)) error(E_INTERN);
  67.  
  68.   w=p->iwidth;
  69.   h=p->iheight;
  70.  
  71.   if(p->mwidth  < 2*w ) error(E_INTERN);
  72.   if(p->mheight < 2*h ) error(E_INTERN);
  73.  
  74.  
  75.   p->iwidth=2*w;
  76.   p->iheight=2*h;
  77.  
  78.  
  79.   for(y=0;y<h;y++)
  80.    {yi=h-1-y;
  81.     optr=p->im+  yi*p->mwidth + (w-1);
  82.     nptr=p->im+2*yi*p->mwidth + (2*w - 2);
  83.  
  84.     nptr[0]=nptr[1]=optr[0];
  85.  
  86.     for(x=1;x<w;x++)
  87.      { optr--; nptr-=2;
  88.        nptr[0]=optr[0];
  89.        nptr[1]=(((int)optr[0])+((int)optr[1])+1)>>1;
  90.      }
  91.     }
  92.  
  93.   for(y=0;y<h-1;y++)
  94.    {optr=p->im + 2*y*p->mwidth;
  95.     nptr=optr+p->mwidth;
  96.     uptr=nptr+p->mwidth;
  97.  
  98.     for(x=0;x<w-1;x++)
  99.      {
  100.       nptr[0]=(((int)optr[0])+((int)uptr[0])+1)>>1;
  101.       nptr[1]=(((int)optr[0])+((int)optr[2])+((int)uptr[0])+((int)uptr[2])+2)>>2;
  102.       nptr+=2; optr+=2; uptr+=2;
  103.      }
  104.     *(nptr++)=(((int)*(optr++))+((int)*(uptr++))+1)>>1;
  105.     *(nptr++)=(((int)*(optr++))+((int)*(uptr++))+1)>>1;
  106.    }
  107.  
  108.  
  109.   optr=p->im + (2*h-2)*p->mwidth;
  110.   nptr=p->im + (2*h-1)*p->mwidth;
  111.   for(x=0;x<w;x++)
  112.    { *(nptr++) = *(optr++);  *(nptr++) = *(optr++); }
  113.  
  114.  }
  115.  
  116.  
  117.  
  118.  
  119. static int testbegin()
  120.  {int i,j;
  121.   for(i=j=0;i<32;i++)
  122.     if(sbuffer[i]==0xff) j++;
  123.  
  124.   return (j>30);
  125.   
  126.  }
  127.  
  128. long Skip4Base()
  129.  {long cd_offset,cd_offhelp;
  130.   
  131.   cd_offset = L_Head + L_Base16 + L_Base4 + L_Base ;
  132.   SEEK(cd_offset+3);          
  133.   EREADBUF;    
  134.   cd_offhelp=((((long)sbuffer[510])<<8)|sbuffer[511]) + 1;
  135.  
  136.   cd_offset+=cd_offhelp;
  137.  
  138.   SEEK(cd_offset);
  139.   EREADBUF;
  140.   while(!testbegin())
  141.    {cd_offset++;
  142.     EREADBUF;
  143.    }
  144.   return cd_offset;
  145.  }
  146.  
  147.  
  148.  
  149.  
  150.  
  151. void planealloc(p,width,height)
  152.   implane *p;
  153.   dim width,height;
  154.  {melde("planealloc\n");
  155.  
  156.   p->iwidth=p->iheight=0;
  157.   p->mwidth=width;
  158.   p->mheight=height;
  159.  
  160.   p->mp = ( p->im = ( uBYTE * ) malloc  (width*height*sizeof(uBYTE)) );
  161.   if(!(p->im)) error(E_MEM);
  162.  }
  163.  
  164.  
  165.  
  166. static void pastequer(gross,px,py,klein)
  167.   implane *gross,*klein;
  168.   dim px,py;
  169.  {dim x,y;
  170.   uBYTE *von,*nach;
  171.  
  172.   if(px+klein->iwidth  > gross->iwidth)  error(E_INTERN);
  173.   if(py+klein->iheight > gross->iheight) error(E_INTERN);
  174.  
  175.   for(y=0;y<klein->iheight;y++)
  176.    { von=klein->im + y * klein->mwidth;
  177.      nach=gross->im + (y+py) * gross->mwidth + px;
  178.      for(x=0;x<klein->iwidth;x++)
  179.         *(nach++)=*(von++);
  180.    }
  181.  }
  182.  
  183.  
  184. static void pastelinks(gross,px,py,klein)
  185.   implane *gross,*klein;
  186.   dim px,py;
  187.  {dim x,y;
  188.   uBYTE *von,*nach;
  189.  
  190.   if(px+klein->iheight > gross->iwidth)  error(E_INTERN);
  191.   if(py+klein->iwidth  > gross->iheight) error(E_INTERN);
  192.  
  193.   for(y=0;y<klein->iwidth;y++)
  194.    { von=klein->im + klein->iwidth - 1 - y;
  195.      nach=gross->im + (y+py) * gross->mwidth + px;
  196.      for(x=0;x<klein->iheight;x++,von+=klein->mwidth)
  197.         *(nach++)=*(von);
  198.    }
  199.  }
  200.  
  201. static void pasterechts(gross,px,py,klein)
  202.   implane *gross,*klein;
  203.   dim px,py;
  204.  {dim x,y;
  205.   uBYTE *von,*nach;
  206.  
  207.   if(px+klein->iheight > gross->iwidth)  error(E_INTERN);
  208.   if(py+klein->iwidth  > gross->iheight) error(E_INTERN);
  209.  
  210.   for(y=0;y<klein->iwidth;y++)
  211.    { von=klein->im + (klein->iheight-1)*klein->mwidth + y;
  212.      nach=gross->im + (y+py) * gross->mwidth + px;
  213.      for(x=0;x<klein->iheight;x++,von-=klein->mwidth)
  214.         *(nach++)=*(von);
  215.    }
  216.  }
  217.  
  218.  
  219. void pastein(gross,xpos,xw,ypos,yh,klein,ori)
  220.   implane *gross,*klein;
  221.   dim xpos,xw,ypos,yh;
  222.   enum TURNS ori;
  223.  {
  224.   switch (ori)
  225.    {
  226.     case T_NONE: pastequer(gross,xpos+(xw-klein->iwidth)/2,ypos+(yh-klein->iheight)/2,klein);
  227.                  break;
  228.     case T_LEFT: pastelinks(gross,xpos+(xw-klein->iheight)/2,ypos+(yh-klein->iwidth)/2,klein);
  229.                  break;
  230.     case T_RIGHT:pasterechts(gross,xpos+(xw-klein->iheight)/2,ypos+(yh-klein->iwidth)/2,klein);
  231.                  break;
  232.     case T_AUTO:
  233.     default: error(E_INTERN);
  234.    }
  235.  }
  236.  
  237.  
  238.  
  239.  
  240. #define cro(p,d) {if(p) {p->im+=d*p->mwidth; p->iheight-=d;}}
  241. #define cru(p,d) {if(p) {p->iheight-=d;}}
  242. #define crl(p,d) {if(p) {p->im+=d; p->iwidth-=d;}}
  243. #define crr(p,d) {if(p) {p->iwidth-=d;}}
  244.  
  245. void cropit(wp,hp,l,c1,c2)
  246.   dim *wp,*hp;
  247.   implane *l,*c1,*c2;
  248. #define w (*wp)
  249. #define h (*hp)
  250.  {dim x,y,s;
  251.   int nl,nr,no,nu;
  252.  
  253.   uBYTE *ptr;
  254.  
  255.   for(y=0,no=0;y<h;y++,no++)
  256.    {for(x=0,ptr=(l->im)+y*(l->mwidth); x<w && (*ptr)<MAX_BLACK ;x++,ptr++);
  257.     if(x<w) break;
  258.    }
  259.   cro(l ,no);
  260.   cro(c1,no);
  261.   cro(c2,no);
  262.   h-=no;
  263.  
  264.  
  265.   for(y=h-1,nu=0;y;y--,nu++)
  266.    {for(x=0,ptr=(l->im)+y*(l->mwidth); x<w && (*ptr)<MAX_BLACK ;x++,ptr++);
  267.     if(x<w) break;
  268.    }
  269.   cru(l ,nu);
  270.   cru(c1,nu);
  271.   cru(c2,nu);
  272.   h-=nu;
  273.  
  274.  
  275.   s=l->mwidth;
  276.  
  277.   for(x=0,nl=0;x<w;x++,nl++)
  278.    {for(y=0,ptr=(l->im)+x; y<h && (*ptr)<MAX_BLACK ; y++, ptr+=s);
  279.     if(y<h) break;
  280.    }
  281.   crl(l ,nl);
  282.   crl(c1,nl);
  283.   crl(c2,nl);
  284.   w-=nl;
  285.   
  286.  
  287.   for(x=w-1,nr=0;x;x--,nr++)
  288.    {for(y=0,ptr=(l->im)+x; y<h && (*ptr)<MAX_BLACK ; y++, ptr+=s);
  289.     if(y<h) break;
  290.    }
  291.   crr(l ,nr);
  292.   crr(c1,nr);
  293.   crr(c2,nr);
  294.   w-=nr;
  295.   
  296.   if (do_melde) 
  297.    {
  298.     if (no || nu || nr || nl )
  299.       fprintf(stderr,"Cut off %d top, %d bottom, %d left, %d right,  new size is %ldx%ld\n",
  300.               no,nu,nl,nr,w,h);
  301.     else
  302.       fprintf(stderr,"Nothing cut off\n");
  303.    }
  304.  
  305. #undef w
  306. #undef h
  307.  }
  308.  
  309.